<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-select
                        style="width:260px;"
                        v-model="search.agentId"
                        filterable
                        remote
                        clearable
                        placeholder="选择机构(可输入名称搜索)"
                        :remote-method="searchAgentHander"
                        :loadings="agentLoading">
                            <el-option
                                v-for="item in agentList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                                :disabled="item.disabled">
                            </el-option>
                    </el-select>
                    <el-input v-model="search.teacherName" placeholder="教练姓名" clearable  class="w165"></el-input>
                    <el-input v-model="search.userName" placeholder="学员姓名" clearable  class="w165"></el-input>
                    <el-date-picker
                            clearable
                            v-model="search.startDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="上课日期"
                        >
                    </el-date-picker>
                    <el-time-select
                        v-model="search.startTime"
                        start="08:00"
                        step="00:15"
                        end="20:00"
                        placeholder="开始时间"
                        >
                    </el-time-select>
                    <el-select v-model="search.courseType" placeholder="课程类型" clearable>
                        <el-option label="30分钟课" :value="1"></el-option>
                        <el-option label="60分钟课" :value="2"></el-option>
                        <el-option label="体验课" :value="4"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="warning" icon="el-icon-plus" @click="table_teacher_add">创建预约【教练优先】</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add">创建预约【时间优先】</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.appointment.list" remoteSort @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="日期" prop="date" align="center" width="120">
                    </el-table-column>
                    <el-table-column label="学员" prop="user" width="160" align="center">
                        <template #default="scope">
                            {{scope.row.user  ? scope.row.user.name + '-' + scope.row.user.account  : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="锁定时段" prop="level" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.startTime + '～' + scope.row.endTime}}
                        </template>
                    </el-table-column>
                    <el-table-column label="学员微电" prop="mobile" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.user  ? scope.row.user.mobile : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="教练" prop="book" width="160" align="center">
                        <template #default="scope">
                            {{scope.row.teacher  ? scope.row.teacher.name + '(' + scope.row.teacher.account + ')' : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="教练微电" prop="mobile" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.teacher  ? scope.row.teacher.mobile : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="机构" prop="agent" width="160" align="center">
                        <template #default="scope">
                            {{scope.row.agent ? scope.row.agent.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="课程分类" prop="courseType" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.courseType.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="交付方式" prop="pay" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.listenType.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="课程名称" prop="course" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.course.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="150">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)" v-if="scope.row.status.value == 0 || (scope.row.status.value == 3 && !scope.row.learn)">编辑</el-button>
                            <el-divider direction="vertical" v-if="scope.row.status.value == 0 || (scope.row.status.value == 3 && !scope.row.learn)"></el-divider>
                            <el-popconfirm title="确定删除该预约吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="text" size="small">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

    <table-dialog v-if="dialog.tableSave" ref="tableDialog" @success="handleSuccess" @closed="dialog.tableSave=false"></table-dialog>

</template>

<script>
import saveDialog from './save.vue'
import tableDialog from './components/tableSave.vue'
export default {
    components: {
        saveDialog,
        tableDialog
    },
    data() {
        return {
            agentLoading: false,
            dialog: {
                save: false,
                tableSave: false,
            },
            search: {
                name: null,
            },
            agentList: [],
        }
    },
    mounted() {
        this.searchAgentHander()
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = page * perPage + index - perPage + 1
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id)
			})
        },
        //添加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //添加
        table_teacher_add() {
            this.dialog.tableSave = true
            this.$nextTick(() => {
                this.$refs.tableDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.appointment.action.delete({ id: row.id }).then(() => {
                this.$refs.table.refresh()
                this.$notify.success("删除成功")
            });
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        },
        searchAgentHander(query) {
            this.agentLoading = true;
            this.$API.agent.listByName.get({name:query}).then((res) => {
                let data = res.data
                let _data = []
                data.forEach(item => {
                    _data.push({
                        id: item.id,
                        name: item.name
                    })
                })
                this.agentList = _data
            }).finally(() => {
                this.agentLoading = false
            });
        },
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
